---
# Defined vars from first maas play to continue onto additional plays.
# these will eventually be put into group_vars

inventory_directory: '{{ inventory_directory }}'
maas_login_profile: '{{ maas_login_profile }}'
maas_login_url: '{{ maas_login_url }}'
maas_login_user: '{{ maas_login_user }}'
maas_match_kvm_name: {{ maas_match_kvm_name }}
maas_virsh_user: '{{ maas_virsh_user }}'

# Defined machines marked as broken
{% if groups['maas_broken_machines'] is defined %}
maas_broken_machines:
{%   for host in groups['maas_broken_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_broken_machines'] is not defined %}
maas_broken_machines: []
{% endif %}

# Defined machines to be Commissioned
{% if groups['maas_commission_machines'] is defined %}
maas_commission_machines:
{%   for host in groups['maas_commission_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_commission_machines'] is not defined %}
maas_commission_machines: []
{% endif %}

# Defined machines to be deployed
{% if groups['maas_deployed_machines'] is defined %}
maas_deployed_machines:
{%   for host in groups['maas_deployed_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_deployed_machines'] is not defined %}
maas_deployed_machines: []
{% endif %}

# Defined machines which have failed
{% if groups['maas_failed_machines'] is defined %}
maas_failed_machines:
{%   for host in groups['maas_failed_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_failed_machines'] is not defined %}
maas_failed_machines: []
{% endif %}

# Defined machines as new
{% if groups['maas_new_machines'] is defined %}
maas_new_machines:
{%   for host in groups['maas_new_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_new_machines'] is not defined %}
maas_new_machines: []
{% endif %}

# Defined machines which are in an unknown power state
{% if groups['maas_power_unknown_machines'] is defined %}
maas_power_unknown_machines:
{%   for host in groups['maas_power_unknown_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_power_unknown_machines'] is not defined %}
maas_power_unknown_machines: []
{% endif %}

# Defined machines which are powered off
{% if groups['maas_powered_off_machines'] is defined %}
maas_powered_off_machines:
{%   for host in groups['maas_powered_off_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_powered_off_machines'] is not defined %}
maas_powered_off_machines: []
{% endif %}

# Defined machines which are powered on
{% if groups['maas_powered_on_machines'] is defined %}
maas_powered_on_machines:
{%   for host in groups['maas_powered_on_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_powered_on_machines'] is not defined %}
maas_powered_on_machines: []
{% endif %}

# Defined machines which are in a ready state
{% if groups['maas_ready_machines'] is defined %}
maas_ready_machines:
{%   for host in groups['maas_ready_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_ready_machines'] is not defined %}
maas_ready_machines: []
{% endif %}

# Defined machines which are to be released
{% if groups['maas_release_machines'] is defined %}
maas_release_machines:
{%   for host in groups['maas_release_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_release_machines'] is not defined %}
maas_release_machines: []
{% endif %}

# Defined machines which are currently in rescue mode
{% if groups['maas_rescue_mode_machines'] is defined %}
maas_rescue_mode_machines:
{%   for host in groups['maas_rescue_mode_machines'] %}
  - '{{ host }}'
{%   endfor %}
{% elif groups['maas_rescue_mode_machines'] is not defined %}
maas_rescue_mode_machines: []
{% endif %}

# Defines KVM VMs to MAAS Relationships
{% if groups['kvm_hosts'] is defined %}
kvm_maas_relationships:
{% for host in groups['kvm_hosts'] %}
{%   for item in hostvars[host]['mac_addresses']['results'] %}
{%     for item2 in (maas_machines['stdout']|from_json) %}
{%       if item2['boot_interface']['mac_address'] == item['stdout'] %}
  - kvm_vm_name: '{{ item['item'] }}'
    kvm_vm_mac: '{{ item['stdout'] }}'
    kvm_vm_host: '{{ host }}'
    maas_fqdn: '{{ item2['fqdn'] }}'
    maas_hostname: '{{ item2['hostname'] }}'
    maas_ips:
{%         for ip in item2['ip_addresses'] %}
      - '{{ ip }}'
{%         endfor %}
{%         for item in maas_power_parameters['results'] %}
{%           if item['item']['system_id'] == item2['system_id'] %}
    maas_current_power_address: '{{ item['stdout'] }}'
{%           endif %}
{%         endfor %}
    maas_desired_power_address: 'qemu+ssh://{{ maas_virsh_user }}@{{ hostvars[host]['ansible_host'] }}/system'
    maas_power_id: '{{ item['item'] }}'
    maas_power_state: '{{ item2['power_state'] }}'
    maas_power_type: '{{ item2['power_type'] }}'
    maas_status: '{{ item2['status'] }}'
    maas_status_name: '{{ item2['status_name'] }}'
    maas_system_id: '{{ item2['system_id'] }}'
{%       endif %}
{%     endfor %}
{%   endfor %}
{% endfor %}
{% elif groups['kvm_hosts'] is not defined %}
kvm_maas_relationships: []
{% endif %}

# Defines boot image sources
maas_boot_image_sources:
{{ maas_boot_image_sources['stdout']|from_json|to_yaml }}

# Defines all currenly available images in MAAS
maas_available_images:
{{ maas_current_available_images['stdout']|from_json|to_yaml }}

# Defines machine power address
{% if maas_power_parameters['results']|length %}
maas_machine_power_parameters:
{% for item in maas_power_parameters['results'] %}
  - system_id: '{{ item['item']['system_id'] }}'
    power_address: '{{ item['stdout'] }}'
{% endfor %}
{% else %}
maas_machine_power_parameters: []
{% endif %}

# Defines all gathered information for every machine in MAAS
{% if '[]' == maas_machines['stdout'] %}
maas_machines: []
{% else %}
maas_machines:
{{ maas_machines['stdout']|from_json|to_yaml }}
{% endif %}
